<?php
	require_once '../../Helpers/DbHelper.php';
	//
	class EnrolmentController
	{
		//Department level types.
		const ELEMENTARY = 'elementary';
		const PRE_ELEMENTARY = 'pre-elementary';
		//Action types.
		const CREATE = 0;
		const UPDATE = 1;
		//User types.
		const STUDENT = 2;
		//
		private $d;
		public function __construct()
		{
			$this->d = new DbHelper();
		}
		public final function enrol($actionType)
		{
		  $departmentLevelType = isset($_POST['pre-elem-form']) ?
                    EnrolmentController::PRE_ELEMENTARY :
                    EnrolmentController::ELEMENTARY;
		  $m = $_POST;
		  //Department Type.
		  if($departmentLevelType == self::ELEMENTARY)
		    $departmentType = 2;
		  else
		    $departmentType = 1;
		  //UserType.
		  $userType = 2;
		  //School Details.
		  $studentId = $m['student-id'];
		  $schoolYear = $m['school-year'];
		  $status = $m['status'];
		  $gradeLevel = $m['grade-level'];
		  //Student Details.
		  $name = $m['name'];
      $birthDate = $m['birth-date'];
      $nickname = $m['nickname'];
      $age = $m['age'];
      $homeNumber = $m['home-number'];
      $address = $m['address'];
      $religion = $m['religion'];
      $nationality = $m['nationality'];
      $sex = $m['sex'];
      $schoolAttended = $m['school-attended'];
      $schoolYearAttended = $m['school-year-attended'];
      $livesWith = $m['lives-with'];
      $livesWithSpecify = $m['lives-with-specify'];
      //Parent's Details.
      $fathersName = $m['fathers-name'];
      $fathersCellphone = $m['fathers-cellphone'];
      $fathersOccupation = $m['fathers-occupation'];
      $fathersOfficeNumber = $m['fathers-office-number'];
      $mothersName = $m['mothers-name'];
      $mothersCellphone= $m['mothers-cellphone'];
      $mothersOccupation = $m['mothers-occupation'];
      $mothersOfficeNumber = $m['mothers-office-number'];
      //Emergency Contacts.
      $emergencyGuardianName1 = $m['emergency-guardian-name'][0];
      $emergencyGuardianCellphone1 = $m['emergency-guardian-cellphone'][0];
      $emergencyGuardianRelationship1 = $m['emergency-guardian-relationship'][0];
      $emergencyGuardianName2 = $m['emergency-guardian-name'][1];
      $emergencyGuardianCellphone2 = $m['emergency-guardian-cellphone'][1];
      $emergencyGuardianRelationship2 = $m['emergency-guardian-relationship'][1];
      //Additional Info.
      $hasAllergies = $m['has-allergies'];
      $allergyDetails = $m['allergy-details'];
      $hasMedicalConditions = $m['has-medical-conditions'];
      $hasMedicalConditionDetails = $m['has-medical-condition-details'];
      $hasBehaviorIssues = $m['has-behavior-issues'];
      $hasBehaviorIssuesDetails = $m['has-behavior-issues-details'];
      $childInfoOthers = $m['child-info-others'];

      $query = '';
      switch($actionType)
      {
        case self::CREATE:
          $query .= "INSERT INTO Students (StudentId,
                UserTypeId,
                DepartmentId,
                GradeLevel,
								SchoolYear,
								Status,
								Name,
								BirthDate,
								Nickname,
								Age,
								HomeNumber,
								Address,
								Religion,
								Nationality,
								Sex,
								SchoolAttended,
								SchoolYearAttended,
								LivesWith,
								LivesWithSpecify,
								FathersName,
								FathersCellphone,
								FathersOccupation,
								FathersOfficeNumber,
								MothersName,
								MothersCellphone,
								MothersOccupation,
								MothersOfficeNumber,
								EmergencyGuardianName1,
								EmergencyGuardianCellphone1,
								EmergencyGuardianRelationship1,
								EmergencyGuardianName2,
								EmergencyGuardianCellphone2,
								EmergencyGuardianRelationship2,
								HasAllergies,
								AllergyDetails,
								HasMedicalConditions,
								HasMedicalConditionDetails,
								HasBehaviorIssues,
								HasBehaviorIssueDetails,
								ChildInfoOthers)
								VALUES ('$studentId',
								'$userType',
								'$departmentType',
								'$gradeLevel',
								'$schoolYear',
								'$status',
								'$name',
								'$birthDate',
								'$nickname',
								'$age',
								'$homeNumber',
								'$address',
								'$religion',
								'$nationality',
								'$sex',
								'$schoolAttended',
								'$schoolYearAttended',
								'$livesWith',
								'$livesWithSpecify',
								'$fathersName',
								'$fathersCellphone',
								'$fathersOccupation',
								'$fathersOfficeNumber',
								'$mothersName',
								'$mothersCellphone',
								'$mothersOccupation',
								'$mothersOfficeNumber',
								'$emergencyGuardianName1',
								'$emergencyGuardianCellphone1',
								'$emergencyGuardianRelationship1',
								'$emergencyGuardianName2',
								'$emergencyGuardianCellphone2',
								'$emergencyGuardianRelationship2',
								'$hasAllergies',
								'$allergyDetails',
								'$hasMedicalConditions',
								'$hasMedicalConditionDetails',
								'$hasBehaviorIssues',
								'$hasBehaviorIssuesDetails',
								'$childInfoOthers')";
                //Insert as new student.
								$stat1 = $this->d->query($query);
								//
								//Add as new user.
								$newStudentQuery = "INSERT INTO Users(UserId,
								                Password,UserTypeId)
								                VALUES('$studentId',
								                '$studentId',".self::STUDENT.")";
								$stat2 = $this->d->query($newStudentQuery);
								//TODO: Create default grades for every successfully enrolled student
								//$rc = new ReportCardController();
          			//$rc->createDefaultGrades($_REQUEST['studentId'], $_REQUEST['schoolYear']);
								$query2 = '';
								switch($departmentLevelType)
      					{
      						case self::PRE_ELEMENTARY:
      						  //$gradeLevel = $m['grade-level'];
      						  $guardianName1 = $m['guardian-name'][0];
      						  $guardianCellphone1 = $m['guardian-cellphone'][0];
      						  $guardianRelationship1 = $m['guardian-relationship'][0];
                    //
      						  $guardianName2 = $m['guardian-name'][1];
      						  $guardianCellphone2 = $m['guardian-cellphone'][1];
      						  $guardianRelationship2 = $m['guardian-relationship'][1];
                    //
      						  $parentExpectations = $m['parent-expectations'];
                    //
      							$query2 .= "INSERT INTO PreElementaryDetails(StudentId,
      							        GuardianName1,
      							        GuardianCellphone1,
      							        GuardianRelationship1,
      							        GuardianName2,
      							        GuardianCellphone2,
      							        GuardianRelationship2,
    							          ParentExpectations)
    							          VALUES('$studentId',
    							          '$guardianName1',
    							          '$guardianCellphone1',
    							          '$guardianRelationship1',
    							          '$guardianName2',
    							          '$guardianCellphone2',
    							          '$guardianRelationship2',
    							          '$parentExpectations')";
    							    //Insert pre-elementary info.
    							    $stat3 = $this->d->query($query2);
      						break;
      						case self::ELEMENTARY:
      						  $parentsAbsenceReason = $m['parents-absence-reason'];
      						  $parentsAbsenceSpecify = $m['parents-absence-specify'];
      						  $takesCare = $m['takes-care'];
      						  $takesCareSpecify = $m['takes-care-specify'];
      						  $parentStatus = $m['married-parents'];
      						  $parentsMarriageType = $m['parents-marriage-type'];
                    //
      							$query2 .= "INSERT INTO ElementaryDetails(StudentId,
      							        ParentsAbsenceReason,
      							        WhoTakesCare,
      							        TakesCareSpecify,
      							        ParentStatus,
      							        ParentsMarriageType)
      							        VALUES('$studentId',
      							        '$parentsAbsenceReason',
      							        '$parentsAbsenceSpecify',
      							        '$takesCare',
      							        '$takesCareSpecify',
      							        '$parentStatus',
      							        '$parentsMarriageType')";
      							//Insert elementary info.
      							$stat3 = $this->d->query($query2);
      						break;
      					}
      					if($stat1 && $stat2 && $stat3)
      					  return true;
        break;
        //
        case self::UPDATE:
          $studentId = $m['student-id'];
          //
          $query = '';
          $query .= "UPDATE Students SET UserTypeId = '$userType',
                  DepartmentId = '$departmentType',
                  GradeLevel = '$gradeLevel',
                  SchoolYear='$schoolYear',
  								Status='$status',
  								Name='$name',
  								BirthDate='$birthDate',
  								Nickname='$nickname',
  								Age='$age',
  								HomeNumber='$homeNumber',
  								Address='$address',
  								Religion='$religion',
  								Nationality='$nationality',
  								Sex='$sex',
  								SchoolAttended='$schoolAttended',
  								SchoolYearAttended='$schoolYearAttended',
  								LivesWith='$livesWith',
  								LivesWithSpecify='$livesWithSpecify',
  								FathersName='$fathersName',
  								FathersCellphone='$fathersCellphone',
  								FathersOccupation='$fathersOccupation',
  								FathersOfficeNumber='$fathersOfficeNumber',
  								MothersName='$mothersName',
  								MothersCellphone='$mothersCellphone',
  								MothersOccupation='$mothersOccupation',
  								MothersOfficeNumber='$mothersOfficeNumber',
  								EmergencyGuardianName1='$emergencyGuardianName1',
  								EmergencyGuardianCellphone1='$emergencyGuardianCellphone1',
  								EmergencyGuardianRelationship1='$emergencyGuardianRelationship1',
  								EmergencyGuardianName2='$emergencyGuardianName2',
  								EmergencyGuardianCellphone2='$emergencyGuardianCellphone2',
  								EmergencyGuardianRelationship2='$emergencyGuardianRelationship2',
  								HasAllergies='$hasAllergies',
  								AllergyDetails='$allergyDetails',
  								HasMedicalConditions='$hasMedicalConditions',
  								HasMedicalConditionDetails='$hasMedicalConditionDetails',
  								HasBehaviorIssues='$hasBehaviorIssues',
  								HasBehaviorIssueDetails='$hasBehaviorIssuesDetails',
  								ChildInfoOthers='$childInfoOthers'
  					WHERE StudentId = $studentId";
  					//
  					$stat1 = $this->d->query($query);
		        //
  					$query2 = '';
  					switch($departmentLevelType)
    				{
    					case self::PRE_ELEMENTARY:
    					  //
  						  $guardianName1 = $m['guardian-name'][0];
  						  $guardianCellphone1 = $m['guardian-cellphone'][0];
  						  $guardianRelationship1 = $m['guardian-relationship'][0];
                //
  						  $guardianName2 = $m['guardian-name'][1];
  						  $guardianCellphone2 = $m['guardian-cellphone'][1];
  						  $guardianRelationship2 = $m['guardian-relationship'][1];
                //
  						  $parentExpectations = $m['parent-expectations'];
                //
  							$query2 .= "UPDATE PreElementaryDetails SET GuardianName1 = '$guardianName1',
  							        GuardianCellphone1 = '$guardianCellphone1',
  							        GuardianRelationship1 = '$guardianRelationship1',
  							        GuardianName2 = '$guardianName2',
  							        GuardianCellphone2 = '$guardianCellphone2',
  							        GuardianRelationship2 = '$guardianRelationship2',
							          ParentExpectations = '$parentExpectations'
							          WHERE StudentId = '$studentId'";
							    //Update pre-elementary info.
							    $stat2 = $this->d->query($query2);
    					break;
    					case self::ELEMENTARY:
    					  //
    					  $parentsAbsenceReason = $m['parents-absence-reason'];
    						$parentsAbsenceSpecify = $m['parents-absence-specify'];
    						$takesCare = $m['takes-care'];
    						$takesCareSpecify = $m['takes-care-specify'];
    						$parentStatus = $m['married-parents'];
    						$parentsMarriageType = $m['parents-marriage-type'];
                //
  							$query2 .= "UPDATE ElementaryDetails SET ParentsAbsenceReason = '$parentsAbsenceReason',
  							        ParentsAbsenceSpecify = '$parentsAbsence',
  							        WhoTakesCare =  '$takesCare',
  							        TakesCareSpecify =  '$takesCareSpecify',
  							        ParentStatus = '$parentStatus',
  							        ParentsMarriageType =  '$parentsMarriageType'
  							        WHERE StudentId = '$studentId'";
  							//Update elementary info.
  							$stat2 = $this->d->query($query2);
    					  break;
  					}
  					if($stat1 && $stat2)
  					  return true;
        break;
      }

		}
		public final function savePaymentLogs($date,$paymentAmount,$receipt)
		{
		  $m = $_POST;
		  $id = $m['student-id'];

		  $query = "INSERT INTO PaymentLogs(StudentId,
		            PaymentDate,
		            PaymentAmount,
		            ReceiptNumber)
		            VALUES('$id',
		            '$date',
		            '$paymentAmount',
		            '$receipt')";
		  return $this->d->query($query);
		}
		public function addRequirements()
		{
		  $m = $_POST;
		  //
		  $requirementName = $_POST['requirement-name'];
		  $studentType = $_POST['student-type'];
		  //
		  $query = "INSERT INTO Requirements(RequirementName,StudentType)
		            VALUES('$requirementName','$studentType')";
		  return $this->d->query($query);
		}
		public function getRequirements()
		{
		  $query = "SELECT * FROM Requirements";
		  return $this->d->query($query);
		}
		public function removeRequirements($id)
		{
		  $query = "DELETE FROM Requirements
		          WHERE Id = '$id'";
		  $this->d->query($query);
		}
		public final function enrol2($departmentLevelType,
									$actionType)
		{
			$m = $_POST;
			//School details.
			$studentId = $m['student-id'];
			$schoolYear = $m['school-year'];
			$status = $m['status'];
			//Student details.
			$name = $m['student-name'];
			$birthDate = $m['birth-date'];
			$nickname = $m['nickname'];
			$age = $m['age'];
			$homePhone = $m['home-phone'];
			$address = $m['address'];
			$religion = $m['religion'];
			$nationality = $m['nationality'];
			$sex = $m['sex'];
			$schoolAttended = $m['school-attended'];
			$schoolYearAttended = $m['school-year-attended'];
			$livesWith = $m['lives-with'];
			$livesWithSpecify = $m['lives-with-specify'];
			$parentsAbsenceReason = $m['parents-absence-reason'];
			$parentsAbsenceReasonSpecify = $m['parents-abscence-reason-specify'];
			$takesCare = $m['takes-care'];
			$takesCareSpecify = $m['takes-care-specify'];
			//Parent's details.
			$fathersName = $m['fathers-name'];
			$fathersCellphone = $m['fathers-cellphone'];
			$fathersOccupation = $m['fathers-occupation'];
			$fathersOfficeNumber = $m['fathers-office-number'];
			$mothersName = $m['mothers-name'];
			$mothersCellphone = $m['mothers-cellphone'];
			$mothersOccupation = $m['mothers-occupation'];
			$mothersOfficeNumber = $m['mothers-office-number'];
			$marriedParents = $m['married-parents'];
			$parentsMarriageType = $m['parents-marriage-type'];
			//Emergency contacts.
			$emergencyGuardianName1 = $m['emergency-guardian-name'][0];
			$emergencyGuardianCellphone1 = $m['emergency-guardian-cellphone'][0];
			$emergencyGuardianRelationShip1 = $m['emergency-guardian-relationship'][0];
			$emergencyGuardianName2 = $m['emergency-guardian-name'][1];
			$emergencyGuardianCellphone2 = $m['emergency-guardian-cellphone'][1];
			$emergencyGuardianRelationShip2 = $m['emergency-guardian-relationship'][1];
			//General information.
			$hasAllergies = $m['has-allergies'];
			$allergyDetails = $m['allergy-details'];
			$hasMedicalConditions = $m['has-medical-conditions'];
			$hasMedicalConditionDetails = $m['has-medical-condition-details'];
			$hasBehaviorIssues = $m['has-behavior-issues'];
			$hasBehaviorIssueDetails = $m['has-behavior-issue-details'];
			$childInfoOthers = $m['child-info-others'];
			//
			$query = '';
			switch($actionType)
			{
				case self::CREATE:
					$query .= 	"INSERT INTO Students (Id,
								SchoolYear,
								YearLevel,
								Status,
								Name,
								BirthDate,
								Nickname,
								Age,
								HomePhone,
								Address,
								Religion,
								Nationality,
								Sex,
								SchoolAttended,
								SchoolYearAttended,
								LivesWith,
								LivesWithSpecify,
								ParentsAbsenceReason,
								ParentsAbsenceReasonSpecify,
								TakesCare,
								TakesCareSpecify,
								FathersName,
								FathersCellphone,
								FathersOccupation,
								FathersOfficeNumber,
								MothersName,
								MothersCellphone,
								MothersOccupation,
								MothersOfficeNumber,
								MarriedParents,
								ParentsMarriageType,
								EmergencyGuardianName1,
								EmergencyGuardianCellphone1,
								EmergencyGuardianRelationship1,
								EmergencyGuardianName2,
								EmergencyGuardianCellphone2,
								EmergencyGuardianRelationship2,
								HasAllergies,
								AllergyDetails,
								HasMedicalConditions,
								HasMedicalConditionDetails,
								HasBehaviorIssues,
								HasBehaviorIssueDetails,
								ChildInfoOthers)
								VALUES ('$studentId',
								'$schoolYear',
								'$yearLevel',
								'$status',
								'$name',
								'$birthDate',
								'$nickname',
								'$age',
								'$homePhone',
								'$address',
								'$religion',
								'$nationality',
								'$sex',
								'$schoolAttended',
								'$schoolYearAttended',
								'$livesWith',
								'$livesWithSpecify',
								'$parentsAbsenceReason',
								'$parentsAbsenceReasonSpecify',
								'$takesCare',
								'$takesCareSpecify',
								'$fathersName',
								'$fathersCellphone',
								'$fathersOccupation',
								'$fathersOfficeNumber',
								'$mothersName',
								'$mothersCellphone',
								'$mothersOccupation',
								'$mothersOfficeNumber',
								'$marriedParents',
								'$parentsMarriageType',
								'$emergencyGuardianName1',
								'$emergencyGuardianCellphone1',
								'$emergencyGuardianRelationship1',
								'$emergencyGuardianName2',
								'$emergencyGuardianCellphone2',
								'$emergencyGuardianRelationship2',
								'$hasAllergies',
								'$allergyDetails',
								'$hasMedicalConditions',
								'$hasMedicalConditionDetails',
								'$hasBehaviorIssues',
								'$hasBehaviorIssueDetails',
								'$childInfoOthers'";
					switch($departmentLevelType)
					{
						case self::PRE_ELEMENTARY:
							$query .= "";
						break;
						case self::ELEMENTARY:
							$query .= "";
						break;
					}
					$query .= ')';
				break;
				//
				case self::UPDATE:
					$i = $m['student-id'];
					$query .= "UPDATE Students SET Id='$studentId',
								SchoolYear='$schoolYear',
								YearLevel='$yearLevel',
								Status='$status',
								Name='$name',
								BirthDate='$birthDate',
								Nickname='$nickname',
								Age='$age',
								HomePhone='$homePhone',
								Address='$address',
								Religion='$religion',
								Nationality='$nationality',
								Sex='$sex',
								SchoolAttended='$schoolAttended',
								SchoolYearAttended='$schoolYearAttended',
								LivesWith='$livesWith',
								LivesWithSpecify='$livesWithSpecify',
								ParentsAbsenceReason='$parentsAbsenceReason',
								ParentsAbsenceReasonSpecify='$parentsAbsenceReasonSpecify',
								TakesCare='$takesCare',
								TakesCareSpecify='$takesCareSpecify',
								FathersName='$fathersName',
								FathersCellphone='$fathersCellphone',
								FathersOccupation='$fathersOccupation',
								FathersOfficeNumber='$fathersOfficeNumber',
								MothersName='$mothersName',
								MothersCellphone='$mothersCellphone',
								MothersOccupation='$mothersOccupation',
								MothersOfficeNumber='$mothersOfficeNumber',
								MarriedParents='$marriedParents',
								ParentsMarriageType='$parentsMarriageType',
								EmergencyGuardianName1='$emergencyGuardianName1',
								EmergencyGuardianCellphone1='$emergencyGuardianCellphone1',
								EmergencyGuardianRelationship1='$emergencyGuardianRelationship1',
								EmergencyGuardianName2='$emergencyGuardianName2',
								EmergencyGuardianCellphone2='$emergencyGuardianCellphone2',
								EmergencyGuardianRelationship2='$emergencyGuardianRelationship2',
								HasAllergies='$hasAllergies',
								AllergyDetails='$allergyDetails',
								HasMedicalConditions='$hasMedicalConditions',
								HasMedicalConditionDetails='$hasMedicalConditionDetails',
								HasBehaviorIssues='$hasBehaviorIssues',
								HasBehaviorIssueDetails='$hasBehaviorIssueDetails',
								ChildInfoOthers='$childInfoOthers'
					WHERE Student.Id = $i";
					//
					switch($departmentLevelType)
					{
						case self::PRE_ELEMENTARY:
							$query .= "";
						break;
						case self::ELEMENTARY:
							$query .= "";
						break;
					}
				break;
			}
			//echo $query;
			$this->d->query($query);
		}
	}
?>